const Koa = require('koa')
const app = new Koa()
const views = require('koa-views')
const json = require('koa-json')
const onerror = require('koa-onerror')
const bodyparser = require('koa-bodyparser')
const logger = require('koa-logger')
const session = require('koa-generic-session')
const redisStore = require('koa-redis')
const koaStatic = require('koa-static')

const userApiRouter = require('./routes/api/user')
const utilsApiRouter = require('./routes/api/utils')
const roleApiRouter = require('./routes/api/role')
const userAndRoleApiRouter = require('./routes/api/userAndRole')
const userInfoApiRouter = require('./routes/api/userInfo')
const areaApiRouter = require('./routes/api/area')
const schoolApiRouter = require('./routes/api/school')
const menuApiRouter = require('./routes/api/menu')
const houseApiRouter = require('./routes/api/house')
const commentApiRouter = require('./routes/api/comment')
const replyApiRouter = require('./routes/api/reply')
const askApiRouter = require('./routes/api/ask')
const leaseApiRouter = require('./routes/api/lease')

const {REDIS_CONF: {host, port}} = require('./conf/db')
const {SESSION_SECRET_KEY} = require('./conf/secretKeys')
const {DIST_FOLDER_PATH} = require('./conf/constant')

// error handler
const onErrorConfig = {
  // redirect: '/error'
}
onerror(app, onErrorConfig)

// middlewares
app.use(bodyparser({
  enableTypes: [ 'json', 'form', 'text' ]
}))
app.use(json())
app.use(logger())
app.use(koaStatic(__dirname + '/public'))
app.use(koaStatic(DIST_FOLDER_PATH))

app.use(views(__dirname + '/views', {
  extension: 'ejs'
}))

// logger
app.use(async (ctx, next) => {
  const start = new Date()
  await next()
  const ms = new Date() - start
  console.log(`${ctx.method} ${ctx.url} - ${ms}ms`)
})

app.keys = [ SESSION_SECRET_KEY ]
app.use(session({
  key: 'damingguo', // cookie键值
  prefix: 'damingguo:sess', // redis前缀
  cookie: {
    path: '/',
    httpOnly: true,
    maxAge: 60 * 60 * 24 * 1000
  },
  ttl: 60 * 60 * 24 * 1000, //默认跟maxAge保持一致
  store: redisStore({
    all: host + ':' + port
  })
}))

// routes
app.use(userApiRouter.routes(), userApiRouter.allowedMethods())
app.use(utilsApiRouter.routes(), utilsApiRouter.allowedMethods())
app.use(roleApiRouter.routes(), roleApiRouter.allowedMethods())
app.use(userAndRoleApiRouter.routes(), userAndRoleApiRouter.allowedMethods())
app.use(userInfoApiRouter.routes(), userInfoApiRouter.allowedMethods())
app.use(areaApiRouter.routes(), areaApiRouter.allowedMethods())
app.use(schoolApiRouter.routes(), schoolApiRouter.allowedMethods())
app.use(menuApiRouter.routes(), menuApiRouter.allowedMethods())
app.use(houseApiRouter.routes(), houseApiRouter.allowedMethods())
app.use(commentApiRouter.routes(), commentApiRouter.allowedMethods())
app.use(replyApiRouter.routes(), replyApiRouter.allowedMethods())
app.use(askApiRouter.routes(), askApiRouter.allowedMethods())
app.use(leaseApiRouter.routes(), leaseApiRouter.allowedMethods())


// error-handling
app.on('error', (err, ctx) => {
  console.error('server error', err, ctx)
})

module.exports = app
